package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.LicenseImgBeen;
import cn.xfdzx.android.apps.shop.bean.LoginBean;
import cn.xfdzx.android.apps.shop.bean.LogininfoBean;
import cn.xfdzx.android.apps.shop.bean.OneBeen;
import cn.xfdzx.android.apps.shop.contract.OtherLoginContract;
import cn.xfdzx.android.apps.shop.contract.ShopInContract;
import cn.xfdzx.android.apps.shop.model.OtherLoginModel;
import cn.xfdzx.android.apps.shop.model.ShopInModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author zf
 * @date 2020/6/27.
 * Description：
 */
public class OtherLoginPresenter extends BasePresenter<OtherLoginContract.View> implements OtherLoginContract.Presenter {

    private OtherLoginContract.Model model;

    public OtherLoginPresenter() {
        model = new OtherLoginModel();
    }


    @Override
    public void loginCode(String mobile, String code) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.loginCode(mobile, code)
                .compose(RxScheduler.<LogininfoBean>Flo_io_main())
                .as(mView.<LogininfoBean>bindAutoDispose())
                .subscribe(new Consumer<LogininfoBean>() {
                    @Override
                    public void accept(LogininfoBean bean) throws Exception {
                        mView.onLoginCodeSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void sendSms(String mobile,String code_type) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.sendSms(mobile,code_type)
                .compose(RxScheduler.<LogininfoBean>Flo_io_main())
                .as(mView.<LogininfoBean>bindAutoDispose())
                .subscribe(new Consumer<LogininfoBean>() {
                    @Override
                    public void accept(LogininfoBean bean) throws Exception {
                        mView.onSendSmsSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void checkBindPhone(String code) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.checkBindPhone(code)
                .compose(RxScheduler.<LogininfoBean>Flo_io_main())
                .as(mView.<LogininfoBean>bindAutoDispose())
                .subscribe(new Consumer<LogininfoBean>() {
                    @Override
                    public void accept(LogininfoBean bean) throws Exception {
                        mView.onCheckBindPhoneSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void wechatLogin(String hash, String mobile,String smsCode) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.wechatLogin(hash,mobile,smsCode)
                .compose(RxScheduler.<LogininfoBean>Flo_io_main())
                .as(mView.<LogininfoBean>bindAutoDispose())
                .subscribe(new Consumer<LogininfoBean>() {
                    @Override
                    public void accept(LogininfoBean bean) throws Exception {
                        mView.wechatLoginSuccess(bean);
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        mView.hideLoading();
                    }
                });
    }

}
